<template>
  <!-- 设置合伙人佣金组件 -->
  <el-dialog :title="'设置' + partnerName + '等级佣金'" :visible.sync="gradeVisible" :close-on-press-escape="false"
    :show-close="false" width="520px">
    <div class="pad-b-10" style="color: red;text-align: center;">合伙人等级业务目前仅支持主播选品小程序</div>
    <div class="pad-b-10">
      <el-switch class="pad-l-20" v-model="partnerGradeAuth" active-color="#13ce66" inactive-color="#ff4949">
      </el-switch>
      <span v-if="!partnerGradeAuth">禁用{{partnerName}}佣金等级</span>
      <span v-if="partnerGradeAuth">启用{{partnerName}}佣金等级</span>
      <el-button type="text" class="pad-l-30" @click="copyCommission">复制等级默认佣金比例
      </el-button>
    </div>
    <div class="pad-l-20 pad-r-20">
      <el-table :data="gradeObject[queryParams.partnerType][queryParams.commisstionType]" style="width: 100%"
        v-loading="loading" element-loading-text="加载中">
        <el-table-column prop="partnerGradeName" :label="partnerName + '等级'"></el-table-column>
        <el-table-column label="佣金">
          <template slot-scope="scope">
            <el-input v-model="scope.row.commissionRate" placeholder="请输入佣金" maxlength="6"
              @keyup.native="scope.row.commissionRate = checkInputNum(scope.row.commissionRate)">
              <template slot="append">%</template>
            </el-input>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="saveGrade">确定</el-button>
      <el-button @click="handleCancel">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import {
    listGoodsPartnerGradeCommission
  } from '@/api/goods/goodsPartnerGradeCommission'
  export default {
    props: {
      onSuccess: Function
    },
    data() {
      return {
        partnerName: '合伙人',
        loading: false,
        queryParams: {
          goodsId: '',
          commisstionType: '1',
          partnerType: '1'
        },
        // 等级对象，分为：合伙人类型和佣金类型
        gradeObject: {
          1: {

          },
          2: {

          }
        },
        partnerGradeAuth: false,
        gradeVisible: false
      }
    },
    created() {

    },
    methods: {
      // 打开设置合伙人等级佣金
      // goodsId 商品id；commissionType：佣金类型;partnerType:合伙人类型；partnerGradeAuth：合伙人等级权限开关 :1-开启，0-关闭
      open(goodsId, commissionType, partnerType, partnerGradeAuth) {
        if (!commissionType) {
          this.msgError('佣金类型不能为空!')
          return
        }
        if (!partnerType) {
          this.msgError('合伙人类型不能为空!')
          return
        }
        if (partnerType === 1) {
          this.partnerName = '一级合伙人'
        } else if (partnerType === 2) {
          this.partnerName = '二级合伙人'
        } else {
          this.partnerName = '合伙人'
        }
        this.gradeVisible = true
        this.queryParams.goodsId = goodsId
        this.queryParams.commissionType = commissionType
        this.queryParams.partnerType = partnerType
        this.partnerGradeAuth = partnerGradeAuth === 1 || false
        var gradeList = this.gradeObject[partnerType][commissionType]
        if (!gradeList || gradeList.length <= 0) {
          this.getGoodsPartnerGradeCommissionList()
        }
      },
      getGoodsPartnerGradeCommissionList() {
        this.loading = true
        this.queryParams.enterpriseId = this.getEnterpriseId()
        listGoodsPartnerGradeCommission(this.queryParams).then(response => {
          this.loading = false
          if (response.isError) return
          var gradeDataList = response.data
          for (var i = 0; i < gradeDataList.length; i++) {
            if (!gradeDataList[i].commissionRate) {
              gradeDataList[i].commissionRate = ''
            }
          }
          this.$set(this.gradeObject[this.queryParams.partnerType], this.queryParams.commisstionType, gradeDataList)
        })
      },
      copyCommission() {
        var gradeList = this.getGradeDataList()
        if (gradeList && gradeList.length > 0) {
          for (var i = 0; i < gradeList.length; i++) {
            gradeList[i].commissionRate = gradeList[i].gradeOriginCommissionRate
          }
          this.$set(this.gradeObject[this.queryParams.partnerType], this.queryParams.commisstionType, gradeList)
        }
      },
      saveGrade() {
        var gradeList = this.getGradeDataList()
        if (this.partnerGradeAuth) {
          if (gradeList && gradeList.length > 0) {
            for (var i = 0; i < gradeList.length; i++) {
              const item = gradeList[i]
              if (item.commissionRate === '' || item.commissionRate === undefined) {
                this.msgError(item.partnerGradeName + '：佣金不能为空')
                return
              }
              if (item.commissionRate * 1 > 100) {
                this.msgError(item.partnerGradeName + '：佣金比例不能大于100')
                return
              }
            }
          } else {
            this.msgError('暂无等级，无法设置佣金')
            return
          }
        } else {
          gradeList = []
        }
        this.onSuccess(this.partnerGradeAuth ? 1 : 0, this.queryParams.partnerType, this.queryParams
          .commisstionType, gradeList)
        this.gradeVisible = false
      },
      handleCancel() {
        this.gradeVisible = false
      },
      // 获取等级数据
      getGradeDataList() {
        var gradeList = this.gradeObject[this.queryParams.partnerType][this.queryParams.commisstionType]
        return gradeList
      }
    }
  }
</script>

<style lang="less" scoped>

</style>
